﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Application = System.Windows.Application;


namespace weighing
{
    public class LangHelper
    {

        #region lang variables

        private static readonly string chiniesLangName = "zh-cn";
        private static readonly string englishLangName = "en";
        private static readonly string LaosLangName = "laos";
        private static readonly string chiniesLangSource = "pack://application:,,,/weighing;component/themes/lang/zh-cn.xaml";
        private static readonly string englishLangSource = $"pack://application:,,,/weighing;component/themes/lang/en.xaml";
        private static readonly string LaosLangSource = $"pack://application:,,,/weighing;component/themes/lang/laos.xaml";

        #endregion

        private static ResourceDictionary _LanResourceDictionary;
        private static ResourceDictionary GetLangResourceDictionary()
        {
            if(_LanResourceDictionary == null)
            {
                var app = ((App)Application.Current);
                _LanResourceDictionary = app?.Resources.MergedDictionaries[2];
            }
           return _LanResourceDictionary;
        }


        public static string Get(LangKeyNames langKey)
        {
            var name = langKey.ToString();

            var res = GetLangResourceDictionary()?[name];

            return res?.ToString();
        }

        #region Langs funcution

        public static void Init()
        {
            if (GlobalData.Config.LangName != chiniesLangName)
            {             
                UpdateLang();
            }
        }


        private static LangType GetLangType(string langName = "")
        {
            if (string.IsNullOrEmpty(langName)) langName = GlobalData.Config.LangName;    
            if (langName == chiniesLangName)
            {
                return LangType.SimpleChinies;

            }
            else if (langName == englishLangName)
            {
                return LangType.English;
            }
            else if (langName == LaosLangName)
            {
                return LangType.Laos;
            }
            else
            {
                return LangType.SimpleChinies;
            }
        }
        public static  void UpdateLang()
        {
            LangType langType = GetLangType();
            ResourceDictionary langDictionary;
            switch (langType)
            {
                case LangType.English:
                    langDictionary = new ResourceDictionary()
                    {
                        Source = new Uri(englishLangSource)
                    };
                    break;
                case LangType.SimpleChinies:
                    langDictionary = new ResourceDictionary()
                    {
                        Source = new Uri(chiniesLangSource)
                    };
                    break;
                case LangType.Laos:
                    langDictionary = new ResourceDictionary()
                    {
                        Source = new Uri(LaosLangSource)
                    };
                    break;
                default:
                    langDictionary = new ResourceDictionary()
                    {
                        Source = new Uri(englishLangSource)
                    };
                    break;
            }
            var langRD = ((App)Application.Current)?.Resources?.MergedDictionaries[2];
            if (langRD != null)
            {
                langRD.MergedDictionaries.Clear();
                langRD.MergedDictionaries.Add(langDictionary);
            }
            try
            {
                App.Current?.MainWindow?.OnApplyTemplate();
                //MainWindow mw = (MainWindow)Application.Current.MainWindow;
                //mw?.InputDataUc?.DataGrid.OnApplyTemplate();
                //mw?.outputDataUc?.DataGrid.OnApplyTemplate();
            }
            catch (Exception)
            {
            }       
        }

        #endregion
    }
}
